#include "kde4-ffmpegthumbcreator.h"
#include <QImage>
#include <libffmpegthumbnailer/videothumbnailer.h>
#include <memory>
using namespace std;
using namespace ffmpegthumbnailer;

extern "C"
{
    //! Factory method for thumbnail creator.
    KDE_EXPORT ThumbCreator *new_creator()
    {
        return new FfmpegThumbCreator;
    }
};

//! Create the actual preview for the file.
bool FfmpegThumbCreator::create(const QString &path, int width, int /*height*/, QImage &img)
{
    // Create thumbnail creator in smart mode.
    auto_ptr<VideoThumbnailer> thumbCreator(
            new VideoThumbnailer(
                    DEFAULT_THUMBNAIL_SIZE, false, true,
                    DEFAULT_THUMBNAIL_QUALITY, true));

    // Preset some values that differ from defaults.
    thumbCreator->setThumbnailSize(width);
    thumbCreator->setSeekPercentage(INITIAL_SEEK_PERCENTAGE);

    // Get a preview frame.
    vector<uint8_t> imageBuffer;
    const QByteArray asc = path.toLocal8Bit();
    thumbCreator->generateThumbnail(string(asc.constData(), asc.length()),
                                    Png,
                                    imageBuffer);

    // Convert vector data into a QImage.
    img = QImage::fromData(QByteArray((char *)&imageBuffer[0], imageBuffer.size()), "png");

    // Don't worry, be happy now.
    return true;
}

// End of file.
